package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.WorkRecord;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportPalletReportResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportWorkPlaceSortPartResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportWorkPlaceSortResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportWorkStationCutResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
import java.util.Map;

@Mapper
public interface WorkRecordMapper extends SyMapper<WorkRecord> {

//    @Select("SELECT p.nest_id AS nestId, r.pallet_number AS plateCode, r.group_number AS groupNumber, r.station_code AS stationCode, p.nest_weight AS cutTotalWeight, p.weight AS plateTotalWeight, p.nest_rate AS rate, nm.total_cut_time AS totalTime, nm.move_time AS workTime, nm.cut_length AS cutTotalLength, np.texture AS texture, np.length AS length, np.width AS width, np.norm AS thickness, r.work_staff_code AS operator, r.work_time AS time FROM tb_mes_work_record r LEFT JOIN tb_mes_plate p ON r.pallet_number=p.mater_code LEFT JOIN tb_third_sinocam_receive_nest_plate np ON r.pallet_number=np.stock_list_no LEFT JOIN tb_third_sinocam_receive_nest_move nm ON np.receive_nest_id=nm.receive_nest_id ${ew.customSqlSegment}")
    @Select("SELECT p.nest_id AS nestId, r.pallet_number AS plateCode, r.group_number AS groupNumber, r.station_code AS stationCode, IFNULL(pallet.totalWeight,p.nest_weight)  AS cutTotalWeight, p.weight AS plateTotalWeight, p.nest_rate AS rate, nm.total_cut_time AS totalTime, nm.move_time AS workTime, nm.cut_length AS cutTotalLength, np.texture AS texture, np.length AS length, np.width AS width, np.norm AS thickness, r.work_staff_code AS operator, r.work_time AS TIME FROM tb_mes_work_record r LEFT JOIN tb_mes_plate p ON r.pallet_number = p.mater_code LEFT JOIN tb_third_sinocam_receive_nest_plate np ON r.pallet_number = np.stock_list_no LEFT JOIN tb_third_sinocam_receive_nest_move nm ON np.receive_nest_id = nm.receive_nest_id LEFT JOIN (SELECT np.plate_code,sum(np.quantity*op.net_weight) totalWeight from tb_third_sinocam_receive_nest_part  np LEFT JOIN tb_order_part op on np.part_id=op.plm_id  GROUP BY  np.plate_code) pallet on pallet.plate_code = r.pallet_number ${ew.customSqlSegment}")
    List<ReportWorkStationCutResVo> plateCutReport(@Param(Constants.WRAPPER) QueryWrapper<WorkRecord> qw);
//    List<ReportWorkStationCutResVo> plateCutReport(@Param("start") String start, @Param("end") String end, @Param("groupCode")String groupCode, @Param(Constants.WRAPPER) QueryWrapper<WorkRecord> qw);

    @Select("SELECT r.attr AS plateCode, p.nest_id AS nestId, SUM(r.total_count) AS partCount, SUM(r.success_count) AS sortCount, SUM(r.fail_count) AS failCount, MIN( r.work_time ) AS reportTime FROM tb_mes_work_record r LEFT JOIN tb_mes_plate p ON r.attr=p.mater_code ${ew.customSqlSegment} GROUP BY r.attr, p.nest_id")
    Page<ReportWorkPlaceSortResVo> plateSortReport(Page<T> page, @Param(Constants.WRAPPER) QueryWrapper<WorkRecord> qw);

    @Select("SELECT r.plmid AS plmId, r.total_count AS partCount, r.success_count AS sortCount, r.fail_count AS failCount, r.work_time AS reportTime FROM tb_mes_work_record r ${ew.customSqlSegment}")
    Page<ReportWorkPlaceSortPartResVo> plateSortPart(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<WorkRecord> qw);

    @Select("delete from tb_mes_work_record ${ew.customSqlSegment} ")
    public void deleteByWrapper(@Param(Constants.WRAPPER) LambdaQueryWrapper<WorkRecord> query);

    @Select("SELECT wrb.plmid AS plmId, wrb.phase_code AS phaseCode, wrb.station_code AS stationCode, wrb.success_count AS successCount, wrb.fail_count AS failCount, " +
            "wrk.pallet_number AS startPallet, wrb.pallet_number AS endPallet, wrb.construct_no AS constructNo, wrb.work_time AS reportDateTime, wrb.work_staff_code AS reporterCode " +
            "FROM tb_mes_work_record wrb LEFT JOIN tb_mes_work_record wrk ON wrb.start_id = wrk.id " +
            " ${ew.customSqlSegment} ")
    Page<ReportPalletReportResVo> getPalletReport(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<WorkRecord> qw);

      @Select("  SELECT awo.erp_code erpCode,awo.work_order_code workOrderCode,awd.phase_code from  tb_mes_work_record record " +
              "  LEFT JOIN tb_aps_work_detail awd on record.aps_work_detail_id=awd.id       " +
              "  LEFT JOIN tb_aps_work_order awo on awd.aps_work_order_id=awo.id " +
              "  where record.plmid=#{plmid}  and awd.phase_code =#{phaseCode} and record.type=2  limit 1")
      Map<String, String> getErpWorkOrderCode(@Param("plmid") String plmid, @Param("phaseCode")  String phaseCode);
      @Select("select  iem.erp_op_code erpOpCode, iem.erp_op erpOp " +
              "from tb_import_erp_work_order_detail  ewod " +
              "left join tb_import_erp_mapping iem on ewod.op_code = iem.erp_op_code and ewod.op = iem.erp_op " +
              "where  ewod.work_order_code =#{workOrderCode} and ewod.material_code =#{erpCode} and iem.mes_work_phase =#{phaseCode} and ewod.deleted = 0 ")
    List<Map<String, Object>> getErpWorkGroup(@Param("workOrderCode") String workOrderCode,@Param("erpCode")  String erpCode,@Param("phaseCode")  String phaseCode);

      @Select("DELETE FROM tb_mes_work_record WHERE id = #{id}")
    void deleteWithId(@Param("id") Integer id);

}
